package cn.tedu.tmall.passport.redis;

import cn.hutool.core.bean.BeanUtil;
import cn.tedu.tmall.common.pojo.po.UserStatePO;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.ResolvableType;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import javax.xml.ws.Action;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
public class RedisTest2 {
    @Autowired
    RedisTemplate redisTemplate;
    @Test
    void redisHash(){
        UserStatePO userStatePO = new UserStatePO();
        userStatePO.setEnable(1);
        userStatePO.setAuthoritesToString("aaaa");
        //Map<String,Object> map = new HashMap<>();
        //map.put("enable",userStatePO.getEnable());
        //map.put("setAuthoritesToString",userStatePO.getAuthoritesToString());
        //使用hutool小工具进行类到map的快速转换
        Map<String, Object> map = BeanUtil.beanToMap(userStatePO);
        HashOperations hash = redisTemplate.opsForHash();
        hash.putAll("user",map);
    }
    @Test
    void entries(){
        HashOperations hash = redisTemplate.opsForHash();
        Map entries = hash.entries("user");
        System.out.println(entries);
        //UserStatePO userStatePO = new UserStatePO();
        //userStatePO.setEnable(Integer.valueOf(entries.get("enable").toString()));
        //userStatePO.setAuthoritesToString(entries.get("setAuthoritesToString").toString());
        UserStatePO userStatePO = BeanUtil.mapToBean(entries, UserStatePO.class, true, null);
        System.out.println(userStatePO);
    }
}
